package 面试;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "come in");
        try { TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace(); }
        return 1024;
    }
}

public class CallableDemo {

    public static void main(String[] args) throws Exception{

        //FutureTask(Callable<> callable);
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());

        new Thread(futureTask, "AAA").start();
        new Thread(futureTask, "BBB").start();

        //要求获得Callable线程的计算结果，若Callable未计算完成，则会阻塞等待它计算完成,
        // 因此一般把这种取Callable线程的结果的语句放在最后面，
        int result02 = futureTask.get();

        System.out.println(Thread.currentThread().getName());
        int result01 = 100;
        System.out.println("result:" + (result01 + result02));

    }



}
